/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.controller;

import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.validate.AddGroup;
import com.comac.ins.common.core.validate.EditGroup;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdDatasourceDataIntegrationTaskBo;
import com.comac.ins.isfd.domain.bo.IsfdDatasourceTaskDataSourceConfigBo;
import com.comac.ins.isfd.domain.vo.IsfdDatasourceDataIntegrationTaskVo;
import com.comac.ins.isfd.domain.vo.IsfdDatasourceTableVo;
import com.comac.ins.isfd.service.IIsfdDatasourceDataIntegrationTaskService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;

/**
 * 数据源数据集成-任务
 *
 * @author wujialin
 * @date 2024-10-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/datasourceDataIntegrationTask")
public class IsfdDatasourceDataIntegrationTaskController extends BaseController {

    private final IIsfdDatasourceDataIntegrationTaskService isfdDatasourceDataIntegrationTaskService;

    /**
     * 查询数据源数据集成任务列表
     */
    @SaCheckPermission("isfd:datasourceDataIntegrationTask:list")
    @GetMapping("/list")
    public TableDataInfo<IsfdDatasourceDataIntegrationTaskVo> list(IsfdDatasourceDataIntegrationTaskBo bo, PageQuery pageQuery) {
        return isfdDatasourceDataIntegrationTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出数据源数据集成任务列表
     */
    @SaCheckPermission("isfd:datasourceDataIntegrationTask:export")
    @Log(title = "数据源数据集成任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IsfdDatasourceDataIntegrationTaskBo bo, HttpServletResponse response) {
        List<IsfdDatasourceDataIntegrationTaskVo> list = isfdDatasourceDataIntegrationTaskService.queryList(bo);
        ExcelUtil.exportExcel(list, "数据源数据集成任务", IsfdDatasourceDataIntegrationTaskVo.class, response);
    }

    /**
     * 获取数据源数据集成任务详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:datasourceDataIntegrationTask:query")
    @GetMapping("/{id}")
    public R<IsfdDatasourceDataIntegrationTaskVo> getInfo(@NotNull(message = "主键不能为空")
                                                          @PathVariable Long id) {
        return R.ok(isfdDatasourceDataIntegrationTaskService.queryById(id));
    }

    /**
     * 新增数据源数据集成任务
     */
    @SaCheckPermission("isfd:datasourceDataIntegrationTask:add")
    @Log(title = "数据源数据集成任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IsfdDatasourceDataIntegrationTaskBo bo) {
        return toAjax(isfdDatasourceDataIntegrationTaskService.insertByBo(bo));
    }

    /**
     * 修改数据源数据集成任务
     */
    @SaCheckPermission("isfd:datasourceDataIntegrationTask:edit")
    @Log(title = "数据源数据集成任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IsfdDatasourceDataIntegrationTaskBo bo) {
        return toAjax(isfdDatasourceDataIntegrationTaskService.updateByBo(bo));
    }

    /**
     * 删除数据源数据集成任务
     *
     * @param id 主键串
     */
    @SaCheckPermission("isfd:datasourceDataIntegrationTask:remove")
    @Log(title = "数据源数据集成任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(isfdDatasourceDataIntegrationTaskService.deleteWithValidById(id, true));
    }

    /**
     * 数据库中的所有表名
     */
    @SaCheckPermission("isfd:datasourceDataIntegrationTask:query")
    @PostMapping("/datasource/table")
    public R<List<IsfdDatasourceTableVo>> getTablesByDataSource(@Validated(AddGroup.class) @RequestBody IsfdDatasourceTaskDataSourceConfigBo bo) {
        return R.ok(isfdDatasourceDataIntegrationTaskService.getTablesByDataSource(bo));
    }
}
